package com.wbpay.sdk.utils;

import android.content.Context;
import android.text.TextUtils;

import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.util.LinkedHashMap;
import java.util.Map;

public class UrlHelper {
    private static final String MD5_ENCTYPT = "abc123def";

    public static String addQueryParams(Context context, String url, Map<String, String> params) {
        StringBuilder sb = new StringBuilder();
        if (params != null) {
            for (Map.Entry<String, String> entry : params.entrySet()) {
                String key = entry.getKey();
                String value = entry.getValue();
                if (!TextUtils.isEmpty(value)) {
                    sb.append(key);
                    sb.append("=");
                    sb.append(value);
                    sb.append("&");
                }
            }
        }
        sb.append("rnd").append("=").append(System.currentTimeMillis());
        sb.append("&");
        sb.append("sha").append("=").append(Utils.getSign(context));
        String query = null;
        try {
            URI uri = new URI(url);
            query = uri.getQuery();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        if (!TextUtils.isEmpty(query)) {
            query = query + "&" + sb.toString();
        } else {
            query = sb.toString();
        }
        String md5String = Utils.string2MD5(query + MD5_ENCTYPT);
        String newParams = query + "&sign=" + md5String;
        String urlPath = url.split("\\?")[0];
        url = urlPath + "?" + encodeParams(newParams);
        return url;
    }

    /**
     * 对Query参数进行编码
     *
     * @param params
     * @return
     */
    private static String encodeParams(String params) {
        if (TextUtils.isEmpty(params)) {
            return null;
        }
        /**创建一个有序的hashmap*/
        Map<String, String> mapRequest = new LinkedHashMap<String, String>();
        String[] arrSplit = null;
        String strUrlParam = params;
        String[] arrSplitEqual = null;
        arrSplit = strUrlParam.split("[&]");
        for (String strSplit : arrSplit) {
            arrSplitEqual = strSplit.split("[=]");
            if (arrSplitEqual.length > 1) {
                try {
                    mapRequest.put(arrSplitEqual[0], URLEncoder.encode(arrSplitEqual[1], "utf-8"));
                } catch (Exception e) {
                }
            } else {
                if (arrSplitEqual[0] != "") {
                    mapRequest.put(arrSplitEqual[0], "");
                }
            }
        }
        StringBuilder sb = new StringBuilder();
        for (Map.Entry<String, String> entry : mapRequest.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
            if (!TextUtils.isEmpty(value)) {
                sb.append(key);
                sb.append("=");
                sb.append(value);
                sb.append("&");
            }
        }
        if (sb.length() > 0) {
            sb.deleteCharAt(sb.length() - 1);
        }
        return sb.toString();
    }
}
